<template>
  <div>
    <!-- 搜索栏 -->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <el-input v-model="searchModel.goodsName" placeholder="物品名称" clearable />
          <el-input v-model="searchModel.goodsOwner" placeholder="物品所属者" clearable />
          <el-button type="primary" round icon="el-icon-search" @click="getGoodsList">查询</el-button>
        </el-col>
        <el-col :span="4" align="right">
          <el-button type="primary" icon="el-icon-plus" circle @click="openEditUI(null)" />
        </el-col>
      </el-row>
    </el-card>

    <!-- 渲染列表 -->
    <el-card>
      <el-table :data="goodsList" stripe style="width: 100%">
        <el-table-column type="index" label="#" width="80">
          <template slot-scope="scope">
            {{ (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="goodsId" label="订单号" width="110" />
        <el-table-column prop="goodsOwner" label="物品所属者" width="110" />
        <el-table-column prop="goodsName" label="物品名称" width="110" />
        <el-table-column prop="goodsPrice" label="物品价格" width="110" />
        <el-table-column prop="goodsNum" label="物品数量" width="110" />
        <el-table-column prop="goodsCount" label="已购数量" width="130" />
        <el-table-column prop="goodsDesc" label="物品描述" width="130" />
        <el-table-column prop="goodsBuyer" label="购买者" width="130" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="min" circle @click="openEditUI(scope.row.goodsId)" />
            <el-button type="danger" icon="el-icon-delete" size="min" circle @click="deleteGoods(scope.row)" />
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="searchModel.pageNo"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!--新增物品-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clearForm">
      <el-form ref="goodsFormRef" :model="goodsFrom" :rules="rules">
        <el-form-item prop="goodsOwner" label="物品所属者" :label-width="formLabelWidth">
          <el-input v-model="goodsFrom.goodsOwner" auto-complete="off" />
        </el-form-item>
        <el-form-item prop="goodsName" label="物品名称" :label-width="formLabelWidth">
          <el-input v-model="goodsFrom.goodsName" auto-complete="off" />
        </el-form-item>
        <el-form-item prop="goodsPrice" label="物品价格" oninput="value=value.replace(/[^\d]/g,'')" :label-width="formLabelWidth">
          <el-input v-model="goodsFrom.goodsPrice" auto-complete="off" @input="(v)=>(goodsFrom.goodsPrice=v.replace(/[^\d.]/g,''))" />
        </el-form-item>
        <el-form-item prop="goodsNum" label="物品数量" oninput="value=value.replace(/[^\d]/g,'')" :label-width="formLabelWidth">
          <el-input v-model="goodsFrom.goodsNum" auto-complete="off" @input="(v)=>(goodsFrom.goodsNum=v.replace(/[^\d.]/g,''))" />
        </el-form-item>
        <el-form-item prop="goodsDesc" label="物品描述" :label-width="formLabelWidth">
          <el-input v-model="goodsFrom.goodsDesc" auto-complete="off" />
        </el-form-item>
        <el-form-item class="hide" prop="goodsFlag" label="是否被买" :label-width="formLabelWidth">
          <el-input v-model="goodsFrom.goodsFlag" :placeholder="0" auto-complete="off" readonly />
        </el-form-item>
        <el-form-item class="hide" prop="goodsCount" label="已购数量" :label-width="formLabelWidth">
          <el-input v-model="goodsFrom.goodsCount" :placeholder="0" auto-complete="off" readonly />
        </el-form-item>
        <el-form-item class="hide" prop="goodsBuyer" label="购买者" :label-width="formLabelWidth">
          <el-input v-model="goodsFrom.goodsBuyer" :placeholder="'暂无'" auto-complete="off" readonly />
        </el-form-item>
        <el-form-item class="hide" prop="goodsMessage" label="信息" :label-width="formLabelWidth">
          <el-input v-model="goodsFrom.goodsMessage" :placeholder="'无'" auto-complete="off" readonly />
        </el-form-item>
        <el-form-item class="hide" prop="chatMsg" label="聊天信息" :label-width="formLabelWidth">
          <el-input v-model="goodsFrom.chatMsg" :placeholder="''" auto-complete="off" readonly />
        </el-form-item>
        <el-form-item class="hide" prop="chatCount" label="聊天信息" :label-width="formLabelWidth">
          <el-input v-model="goodsFrom.chatCount" :placeholder="1" auto-complete="off" readonly />
        </el-form-item>
        <el-form-item class="hide" prop="chatFlag" label="正在聊天的" :label-width="formLabelWidth">
          <el-input v-model="goodsFrom.chatFlag" :placeholder="0" auto-complete="off" readonly />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveGoods">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import goodsApi from '@/api/goodsManage'
export default {
  data() {
    return {
      goodsList: [],
      formLabelWidth: '130px',
      goodsFrom: {},
      dialogFormVisible: false,
      title: '',
      total: 0,
      searchModel: {
        pageNo: 1,
        pageSize: 5
      },
      rules: {
        goodsOwner: [
          { required: true, message: '请输入物品所属者', tigger: 'blur' },
          { min: 3, max: 10, message: '请输入1~10字符', tigger: 'blur' }
        ],
        goodsName: [
          { required: true, message: '请输入物品名称', tigger: 'blur' },
          { min: 1, max: 10, message: '请输入1~10字符', tigger: 'blur' }
        ],
        goodsDesc: [
          { required: true, message: '请输入物品描述', tigger: 'blur' },
          { min: 1, max: 35, message: '请输入1~35字符', tigger: 'blur' }
        ],
        goodsNum: [
          { required: !0, message: '不能为空' }
        ],
        goodsPrice: [
          { required: !0, message: '不能为空' }
        ]
      }
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    deleteGoods(goods) {
      this.$confirm(`您确认删除订单号${goods.goodsId} ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        goodsApi.deleteGoodsById(goods.goodsId).then(response => {
          this.$message({
            type: 'success',
            message: response.message
          })
          this.getGoodsList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    saveGoods() {
      this.$refs.goodsFormRef.validate((valid) => {
        if (valid) {
          // 提交请求到后台
          if (this.goodsFrom.goodsId == null && this.goodsFrom.goodsId === undefined) {
            this.$set(this.goodsFrom, 'goodsFlag', 0)
            this.$set(this.goodsFrom, 'goodsCount', 0)
            this.$set(this.goodsFrom, 'goodsBuyer', '暂无')
            this.$set(this.goodsFrom, 'goodsMessage', '无')
            this.$set(this.goodsFrom, 'chatMsg', '')
            this.$set(this.goodsFrom, 'chatCount', 1)
            this.$set(this.goodsFrom, 'chatFlag', 0)
          }
          goodsApi.saveGoods(this.goodsFrom).then(response => {
            this.$message({
              message: response.message,
              type: 'success'
            })
            // 关闭对话框
            this.dialogFormVisible = false
            // 刷新表格
            this.getGoodsList()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    clearForm() {
      this.goodsFrom = {}
      this.$refs.goodsFormRef.clearValidate()
    },
    openEditUI(id) {
      if (id == null) {
        this.title = '新增订单'
      } else {
        this.title = '修改订单'
        // 根据id查询用户数据
        goodsApi.getGoodsById(id).then(respose => {
          this.goodsFrom = respose.data
        })
      }
      this.dialogFormVisible = true
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize
      this.getGoodsList()
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo
      this.getGoodsList()
    },
    getGoodsList() {
      goodsApi.getGoodsList(this.searchModel).then(response => {
        this.goodsList = response.data.rows
        this.total = response.data.total
      })
    }
  }
}
</script>

<style scopde>
#search .el-input {
  width: 200px;
  margin-right: 20px;
}
.el-dialog .el-input {
  width: 85%;
}
.hide{
  display: none;
}
</style>
